package dao.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import pojo.Goods;
import utils.JDBCUtils;

import java.util.List;

public class GoodsDaoImpl implements dao.GoodsDao {
    private JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDs());

    @Override
    public int goodsAdd(String gname, String gprice, String gnumber) {
        try {
            String sql = "insert  into goods  (gname,gprice,gnumber)  values(?,?,?)";
            int update = jt.update(sql, gname, gprice, gnumber);
            return update;
        } catch (Exception e) {
            return 0;
        }
    }
    @Override
    public List<Goods> findByPage(Integer currentPage, Integer pageSize){
        List<Goods> list = jt.query("select * from tab_student limit ?,?", new BeanPropertyRowMapper<Goods>(Goods.class), (currentPage-1)*pageSize, pageSize);
        return list;
    }

    @Override
    public Number findTotalCount() {
        try {
                Number number = jt.queryForObject("select count(gid) from goods", Number.class);
            return number;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Boolean deleteGoods(String gid) {
        try {
            String sql = "delete from goods where gid=?";
            Goods goods = jt.queryForObject(sql, new BeanPropertyRowMapper<Goods>(Goods.class), gid);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<Goods> goodsShow() {
        String sql = "select  *  from  goods";

        List<Goods> query = jt.query(sql, new BeanPropertyRowMapper<Goods>(Goods.class));
        return query;
    }

    @Override
    public int delete(int gid) {
        try {
            String sql = "delete from goods where gid=?";
            int i = jt.update(sql, gid);
            return i;
        } catch (Exception e) {
            return 0;
        }
    }
}

